package com.zijidelu.luframework.application.service;

import com.zijidelu.luframework.infrastructure.model.api.PageQuery;
import com.zijidelu.luframework.infrastructure.model.api.PageResponse;
import com.zijidelu.luframework.infrastructure.persistence.repository.core.UnifiedRepository;
import com.zijidelu.luframework.base.model.DataModel;
import com.zijidelu.luframework.base.model.PO;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * 基础控制器实现类。
 *
 * @author Ziang.Chen
 * @date: 2024/5/7 13:48
 */
public abstract class AbstractAppService<P extends PO, D extends DataModel> implements AppService<D> {
    @Getter
    @Setter(onMethod_ = {@Autowired})
    private UnifiedRepository<P, D> unifiedRepository;


    @Override
    public D findById(Long id) {
        return unifiedRepository.getDataRepository().findById(id);
    }

    @Override
    public PageResponse<D> findPage(PageQuery<D> query) {
        return unifiedRepository.listPage(query);
    }

    @Override
    public boolean save(List<D> request) {
        return unifiedRepository.getDataRepository().saveBatch(request);
    }

    @Override
    public boolean update(List<D> request) {
        return unifiedRepository.getDataRepository().updateBatchById(request);
    }

    @Override
    public boolean deleteById(Collection<Long> ids) {
        return unifiedRepository.getDataRepository().deleteBatchById(new HashSet<>(ids));
    }
}
